class ModifyEcase < ActiveRecord::Migration
  def self.up
    execute 'ALTER TABLE `ecases` ADD COLUMN `slideshow_id` INT(11) NULL'
    execute 'ALTER TABLE `ecases` CHANGE `overview` `description` VARCHAR(255) NOT NULL'
    execute 'ALTER TABLE `ecases` DROP COLUMN `region_id`'
    slideshows = Slideshow.find_all
    slideshows.each {|s|
      if s.ecase_id
        ecase = Ecase.find_by_id(s.ecase_id)
        if !ecase.nil?
          if ecase.update_attribute("slideshow_id", s.id)
            puts("Slideshow moved to ecase " + ecase.id.to_s)
          else 
            puts("Error during moving slideshow to ecase " + ecase.id.to_s)
          end
        end
       end
    }
    execute 'ALTER TABLE `slideshows` DROP COLUMN `ecase_id`'
  end

  def self.down
    execute 'ALTER TABLE `ecases` DROP COLUMN `slideshow_id`'
    execute 'ALTER TABLE `ecases` CHANGE `description` `overview` VARCHAR(255) NOT NULL'  
 end
end
